package com.li.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.constant.BaseContext;
import com.li.exception.BaseException;
import com.li.mapper.DictDateMapper;
import com.li.mapper.DictTypeMapper;
import com.li.pojo.dto.DictTypeDTO;
import com.li.pojo.dto.DictTypePageDto;
import com.li.pojo.po.DictType;
import com.li.pojo.po.PageResult;
import com.li.service.DictTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-11
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements DictTypeService {
    @Autowired
    private DictTypeMapper dictTypeMapper;
    @Autowired
    private DictDateMapper dictDateMapper;

    @Override
    public void add(DictTypeDTO dictTypeVo) {
        addVerify(dictTypeVo);

        DictType dictType = new DictType();
        BeanUtils.copyProperties(dictTypeVo,dictType);
        dictType.setUpdateBy(BaseContext.getCurrentName());
        dictType.setCreateBy(BaseContext.getCurrentName());
        dictType.setCreateTime(LocalDateTime.now().toString());
        dictType.setUpdateTime(LocalDateTime.now().toString());
        dictType.setStatus("0");
        dictTypeMapper.insert(dictType);
    }

    @Override
    public PageResult findByPage(DictTypePageDto pageDto) {
        if (pageDto.judge()){
            List<DictType> list = this.list();
            return new PageResult(200,"查询成功",null,list,(long)list.size());
        }
        Page<DictType> page = PageHelper.startPage(pageDto.getPageNum(), pageDto.getPageSize());
        if (pageDto.getParams()==null){
           dictTypeMapper.findByPage(pageDto.getDictName(),pageDto.getDictType(),pageDto.getStatus(),null,null);
        }else
        {
         dictTypeMapper.findByPage(pageDto.getDictName(),pageDto.getDictType(),pageDto.getStatus()
                    ,pageDto.getParams().get("beginTime"),pageDto.getParams().get("endTime"));
        }
        return new PageResult(200,"查询成功",null,page.getResult(),page.getTotal());
    }

    @Override
    public DictType findById(Integer dictId) {
        DictType dictType = dictTypeMapper.selectById(dictId);
        return dictType;
    }

    @Override
    public void deleteByIds(List<Integer> dictIds) {
        dictTypeMapper.deleteBatchIds(dictIds);
    }

    @Override
    public void updateDictType(DictTypeDTO dictTypeDTO) {

        updateVerify(dictTypeDTO);

        DictType dictType = new DictType();
        BeanUtils.copyProperties(dictTypeDTO,dictType);
        dictType.setUpdateTime(LocalDateTime.now().toString());
        dictType.setUpdateBy(BaseContext.getCurrentName());
        DictType type = dictTypeMapper.selectById(dictTypeDTO.getDictId());
        dictDateMapper.updateMore(type.getDictType(),dictType.getDictType());
        dictTypeMapper.updateById(dictType);
    }

    @Override
    public void export(DictTypePageDto pageDto, HttpServletResponse response) {
        List<DictType> rows = findByPage(pageDto).getRows();
        String fileName="DictType.xlsx";
        try {
            EasyExcel.write(response.getOutputStream())
                    .head(DictType.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("字典类型表")
                    .doWrite(rows);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //校验字典类型的名称和类型是否重复
    private void addVerify(DictTypeDTO dictTypeVo) {
        QueryWrapper<DictType> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_name", dictTypeVo.getDictName()).or().eq("dict_type", dictTypeVo.getDictType());
        List<DictType> list = dictTypeMapper.selectList(wrapper);
        if (list.size()>0){
            throw new BaseException("字典名称或类型重复");
        }

    }

    //校验字典类型的名称和类型是否重复
    private void updateVerify(DictTypeDTO dictTypeVo) {
        QueryWrapper<DictType> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_name", dictTypeVo.getDictName()).or().eq("dict_type", dictTypeVo.getDictType());
        List<DictType> list = dictTypeMapper.selectList(wrapper);
        if (list.size()>1){
            throw new BaseException("字典名称或类型重复");
        }else if (list.size()==1){
            DictType dictType = list.get(0);
            if (dictType.getDictId().intValue()!=dictTypeVo.getDictId()){
                throw new BaseException("字典名称或类型重复");
            }
        }
    }
}
